import { createRouter, createWebHashHistory } from 'vue-router'
// 引入store
import store from "../store/index.js";

const routes = [

  {
    path: '/',
    name: 'goodsindex',
    component: () => import("../views/HomeView.vue")
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import("../components/Login.vue")
  },
  {
    path: '/slidedetail',
    name: "slideDetail",
    component: () => import("../components/slideDetail.vue")
  },
  {
    path: '/more/:id',
    name: "showMore",
    component: () => import("../components/showMore.vue")
  },
  {
    path: '/goodsdetail',
    name: "goodsDetail",
    component: () => import("../components/goodsDetail.vue")
  },
  {
    path: '/cart',
    name: "userCart",
    component: () => import("../components/userCart.vue")
  }, {
    path: '/order',
    name: "Order",
    component: () => import("../components/Order.vue")
  },
  {
    path: '/myself',
    name: "mySelf",
    component: () => import("../components/mySelf.vue")
  },
  {
    path: '/test',
    name: "Test",
    component: () => import("../components/Test.vue")
  },
  {
    path: '/search',
    name: 'ShowSearch',
    component: () => import("../components/ShowSearch.vue")
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import("../components/Register.vue")
  },
  {
    path: '/forgot',
    name: 'forGot',
    component: () => import("../components/forGot.vue")
  }

]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
  /*
  * to:去哪个页面
  * from：从哪个页面
  * next：只有执行next，页面才会跳转
  *
  * */
  //  判断用户是否登录
  // console.log("store", store.state.userInfo.user)
  const uInfo = store.state.userInfo.user
  if (!uInfo.phone) {
    //  未登录，跳转到登录页
    //  未注册，跳转到注册页
    if (to.path === "/register" || to.path === "/login" || to.path === "/forgotPwd" || to.path === "/goodsdetail") {
      next()
      return
    }
  }
  //不为空直接放行
  next()

})

export default router
